package de.bazinga.rotw.menu;

import java.util.Iterator;
import java.util.Vector;
import java.util.concurrent.Callable;

import com.jme.image.Texture;
import com.jme.math.Vector3f;
import com.jme.renderer.ColorRGBA;
import com.jme.renderer.Renderer;
import com.jme.scene.Spatial;
import com.jme.scene.shape.Quad;
import com.jme.scene.state.TextureState;
import com.jme.util.GameTaskQueueManager;
import com.jme.util.TextureManager;
import com.jmex.game.state.GameStateManager;

import de.bazinga.rotw.game.Game;
import de.bazinga.rotw.game.RotwGameState;
import de.bazinga.rotw.menu.item.MenuItem;
import de.bazinga.rotw.network.LANGames;
import de.bazinga.rotw.network.NetworkScan;

public class LANMenu extends Menu {

	private ColorRGBA[] colorList = {/*ColorRGBA.red,ColorRGBA.brown,ColorRGBA.blue,*/ColorRGBA.green,ColorRGBA.white};
	private String[] colorNames = {/*"red.png","brown.png","blue.png",*/"green.png","white.png"};
	private String[] playerTexture = {/*"red","brown","blue",*/"green","white"};
	private int currColorIndex = 0;
	
	private NetworkScan ns = new NetworkScan(this);
	private int serverScrollStatus = 0;
	
	
	public LANMenu() {
		super("lanMenu");
	}
	
	
	protected void init() {		
		// Background
		Quad bgQuad = new Quad("bg",
				Game.getGame().getDisplay().getWidth(), 
				Game.getGame().getDisplay().getHeight());
		
		bgQuad.setRenderQueueMode(Renderer.QUEUE_ORTHO);   
	 	
		bgQuad.setLocalTranslation(new Vector3f(
				Game.getGame().getDisplay().getWidth()/2,
				Game.getGame().getDisplay().getHeight()/2,
				0));
		
		TextureState bg = Game.getGame().getDisplay().getRenderer().createTextureState();
		bg.setEnabled(true);
		bg.setTexture(TextureManager.loadTexture(RotwGameState.class
				.getClassLoader().getResource("background/menuBg.png"),
				Texture.MinificationFilter.BilinearNearestMipMap,
				Texture.MagnificationFilter.Bilinear));
		
		bgQuad.setRenderState(bg);
		
		bgQuad.setLightCombineMode(Spatial.LightCombineMode.Off);
		bgQuad.updateRenderState();
		
		getRootNode().attachChild(bgQuad);
	}
	
	
	protected void initMenuItems() {
		otherItems.put("colorString", 
				new MenuItem("color.png",
						"",
						2f,
						150,
						Game.getGame().getDisplay().getHeight() - 150,
						250,100));

		menuItems.put("color", 
				new MenuItem("color/green.png",
						"color",
						2f,
						350,
						Game.getGame().getDisplay().getHeight() - 150,
						64,64));
		
		/*textItems.put("username", 
				new TextItem("Username: ",
						"username", 
						ColorRGBA.yellow,
						ColorRGBA.red,
						2f, 
						50, 
						Game.getGame().getDisplay().getHeight() - 250));
		*/
		
		menuItems.put("connect", 
				new MenuItem("network/connect.png",
						"connect",
						2f,
						150,
						Game.getGame().getDisplay().getHeight() - 250,
						250,100));


		menuItems.put("networkScan", 
				new MenuItem("network/scan.png",
						"networkScan",
						2f,
						150,
						Game.getGame().getDisplay().getHeight() - 350,
						250,100));

		menuItems.put("abortScan", 
				new MenuItem("network/abort.png",
						"abortScan",
						2f,
						150,
						Game.getGame().getDisplay().getHeight() - 450,
						250,100));

		
		menuItems.put("back", 
				new MenuItem("back.png",
						"back",
						2f,
						150,
						Game.getGame().getDisplay().getHeight()-550,
						250,100));


		


		menuItems.put("prevServer", 
				new MenuItem("Up",
						"prevServer",
						ColorRGBA.red,
						2f,
						350,
						Game.getGame().getDisplay().getHeight() - 250));
		
		menuItems.put("serverList1", 
				new MenuItem("",
						"serverList1",
						ColorRGBA.red,
						2f,
						350,
						Game.getGame().getDisplay().getHeight() - 300));

		menuItems.put("serverList2", 
				new MenuItem("",
						"serverList2",
						ColorRGBA.red,
						2f,
						350,
						Game.getGame().getDisplay().getHeight() - 350));

		menuItems.put("serverList3", 
				new MenuItem("",
						"serverList3",
						ColorRGBA.red,
						2f,
						350,
						Game.getGame().getDisplay().getHeight() - 400));

		menuItems.put("serverList4", 
				new MenuItem("",
						"serverList4",
						ColorRGBA.red,
						2f,
						350,
						Game.getGame().getDisplay().getHeight() - 450));
		


		menuItems.put("nextServer", 
				new MenuItem("Down",
						"nextServer",
						ColorRGBA.red,
						2f,
						350,
						Game.getGame().getDisplay().getHeight() - 500));
	}
	
	protected void handleClick(String item) {
		if(menuItems.containsKey(item)) {
			if(item.equals("back")) {
				ns.stopScanning();
				GameStateManager.getInstance().getChild("gameMenu").setActive(true);
				this.setActive(false);
			}else if(item.equals("connect")) {
				// Try to connect with an empty ip
				connect("");
				
			}else if(item.equals("color")) {
				
				if(currColorIndex == colorList.length-1) {
					// Last Element, so take first
					currColorIndex = 0;
				}else{
					// NExt Element
					currColorIndex++;
				}
				menuItems.get(item).setText("color/"+colorNames[currColorIndex]);
			}else if(item.equals("networkScan")) {
				ns.scanLanForHosts();
			}else if(item.equals("abortScan")) {
				ns.stopScanning();
			}else if(item.startsWith("serverList")) {
				ns.stopScanning();
				connect(menuItems.get(item).getText());
			}else if(item.equals("prevServer")) {

				serverScrollStatus -= 4;
				
				if(serverScrollStatus < 0) serverScrollStatus = 0;
				updateServerList();
			}else if(item.equals("nextServer")) {
				
				if(LANGames.getInstance().getGames().size() >= (serverScrollStatus + 4) ) {
					serverScrollStatus += 4;
				}
				updateServerList();
				
			}
		}
	}

	
	
	private void connect(String ip) {
		//as soon as i disconnect the server-player doesnt get the right position
		((RotwGameState)GameStateManager.getInstance().getChild("gameState")).disconnect();
		
		
		// Set Bullet-Color
		((RotwGameState)GameStateManager.getInstance().getChild("gameState")).
			setPlayerBulletColor(colorList[currColorIndex]);

		// Set Player-Color
		((RotwGameState)GameStateManager.getInstance().getChild("gameState")).getPlayer()
			.setTextureColor(playerTexture[currColorIndex]);
		
		// Set Player-Name
		((RotwGameState)GameStateManager.getInstance().getChild("gameState"))
			.getPlayer().setPlayerName(
					((RotwGameState)GameStateManager.getInstance().getChild("gameState")).getProps().getProperty("playerName"));
		
		
		
		final String serverIp = ip;
		
		this.setActive(false);
		GameStateManager.getInstance().getChild("loadingMenu").setActive(true);
		
		GameTaskQueueManager.getManager().update(new Callable<Void>(){

			public Void call() throws Exception {
		
				((LoadingState)GameStateManager.getInstance().getChild("loadingMenu")).startClient(serverIp);
				return null;
			}
		});
	}
	
	protected void handleHover(MenuItem hoverItem) {
		Iterator<MenuItem> it = menuItems.values().iterator();
		while(it.hasNext()) {
			MenuItem item = it.next();
			if(hoverItem == item) {
				
				if(item == menuItems.get("prevLevel")) {
					item.setText("previousRed.png");
				}else if(item == menuItems.get("nextLevel")) {
					item.setText("nextRed.png");
				}else if(item == menuItems.get("newGame")) {
					item.setText("newGameHover.png");
				}else if(item == menuItems.get("continue")) {
					item.setText("continueHover.png");
				}else if(item == menuItems.get("back")) {
					item.setText("backHover.png");
				}else if(item == menuItems.get("connect")) {
					item.setText("network/connectHover.png");
				}else if(item == menuItems.get("networkScan")) {
					item.setText("network/scanHover.png");
				}else if(item == menuItems.get("abortScan")) {
					item.setText("network/abortHover.png");
				}else{
					item.setTextColor(ColorRGBA.yellow);
				}
			}else{
				if(item == menuItems.get("prevLevel")) {
					item.setText("previous.png");
				}else if(item == menuItems.get("nextLevel")) {
					item.setText("next.png");
				}else if(item == menuItems.get("newGame")) {
					item.setText("newGame.png");
				}else if(item == menuItems.get("continue")) {
					item.setText("continue.png");
				}else if(item == menuItems.get("back")) {
					item.setText("back.png");
				}else if(item == menuItems.get("connect")) {
					item.setText("network/connect.png");
				}else if(item == menuItems.get("networkScan")) {
					item.setText("network/scan.png");
				}else if(item == menuItems.get("abortScan")) {
					item.setText("network/abort.png");
				}else{
					item.setTextColor(item.getTextColor());
				}
			}
		}
	}
	
	public void updateServerList() {
		Vector<String> lanGames = LANGames.getInstance().getGames();

		// Upper Limit
		int upper = serverScrollStatus + 4;
		if(upper > lanGames.size()) {
			upper = lanGames.size();
		}
		
		int currentIndex = 1;
		for(int i=serverScrollStatus;i < upper; i++) {
			
				menuItems.get("serverList"+currentIndex).setText(
						lanGames.get(i));
				currentIndex++;
			
		}
	}
}